package com.warehouse.controller;

import com.warehouse.dto.ActionResult;
import com.warehouse.utils.UploadUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

@Api(tags = "图片上传")
@Slf4j
@RestController
@RequestMapping("/api")
public class ImageController {
    UploadUtils uploadUtils=new UploadUtils();
    @ResponseBody
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST)
    public ActionResult uploadImg(@RequestParam MultipartFile file) {
        ActionResult actionResult=new ActionResult();
        if (file.isEmpty()) {
            actionResult.setMessage("文件不能为空");
            return actionResult;
        }
        try {
            byte[] bytes = file.getBytes();
            String imageName = UUID.randomUUID().toString();
            try {
                //使用base64方式上传到七牛云
                String url = uploadUtils.put64image(bytes, imageName);
                log.info("上传地址为----：" + url);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            actionResult.setMessage("上传图片异常");
            return actionResult;
        }
        actionResult.setMessage("上传成功");
        return actionResult;
    }

}
